﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// 
    /// </summary>
    public class Multiplizieren : BaseImageFilter, ITwoSourceFilter
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Multiplizieren"/> class.
        /// </summary>
        public Multiplizieren()
        {
        }

        internal Multiplizieren(Multiplizieren _multiplizieren)
        {

        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            Process((UnsafeBitmap)_bitmap.Clone(), _bitmap);
            return _bitmap;
        }

        #region ITwoSourceFilter Members

        /// <summary>
        /// Processes the specified _bitmap source.
        /// </summary>
        /// <param name="_bitmapSource">The _bitmap source.</param>
        /// <param name="_bitmapDest">The _bitmap dest.</param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bitmapSource, UnsafeBitmap _bitmapDest)
        {
            UnsafeBitmap bitmapSource = _bitmapSource;
            UnsafeBitmap bitmapDest = _bitmapDest;

            Color colorSource = Color.Transparent;
            Color colorDest = Color.Transparent;

            int y = 0;
            int x = 0;

            int nHeight = bitmapSource.Height;
            int nWidth = bitmapSource.Width;

                for (y = 0; y < nHeight; y++)
                {
                    for (x = 0; x < nWidth; x++)
                    {
                        colorSource = bitmapSource.GetPixel(x, y);
                        colorDest = bitmapDest.GetPixel(x, y);
                        int nR = colorSource.R * colorDest.R;
                        int nG = colorSource.G * colorDest.G;
                        int nB = colorSource.B * colorDest.B;

                        nR = nR >> 8;
                        nG = nG >> 8;
                        nB = nB >> 8;
                        colorDest = Color.FromArgb(nR, nG, nB);
                        bitmapDest.SetPixel(x, y, colorDest);
                    }
                }
            bitmapSource.Dispose();
            bitmapDest.Dispose();
            return _bitmapDest;
        }

                /// <summary>
        /// Processes the specified _bitmap source.
        /// </summary>
        /// <param name="_bitmapSource">The _bitmap source.</param>
        /// <param name="_bitmapDest">The _bitmap dest.</param>
        /// <returns></returns>
        public UnsafeBitmap Process(UnsafeBitmap _bitmapSource, int _nConstant)
        {
            UnsafeBitmap bitmapSource = _bitmapSource;

            Color colorSource = Color.Transparent;
            Color colorDest = Color.Transparent;

            int y = 0;
            int x = 0;

            int nHeight = bitmapSource.Height;
            int nWidth = bitmapSource.Width;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    colorSource = bitmapSource.GetPixel(x, y);
                    colorDest = Color.FromArgb(colorSource.ToArgb() * _nConstant);
                    _bitmapSource.SetPixel(x, y, colorDest);
                }
            }

            return _bitmapSource;
        }

        #endregion

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Multiplizieren(this);
        }
    }
}
